package yxy.game.pm2.active.module.openservicerank;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.module.AreaModule;

/**
 * 缓存记录冲榜奖励是否发放
 *
 * @ClassName OpenServiceHeroRushModule
 * @Author jingtao-zheng
 * @Date 2022/7/11 10:15
 * @Version 1.0
 **/
public class OpenServiceRushModule extends AreaModule {
    private boolean isAward;//是否发已经放奖励
    private DateTime rewardTime;//奖励发放时间
    private int rewardtype;//发奖的类型
    private int serverid;//服务器

    public boolean isAward() {
        return isAward;
    }

    public void setAward(boolean award) {
        isAward = award;
    }

    public DateTime getRewardTime() {
        return rewardTime;
    }

    public void setRewardTime(DateTime rewardTime) {
        this.rewardTime = rewardTime;
    }

    public int getRewardtype() {
        return rewardtype;
    }

    public void setRewardtype(int rewardtype) {
        this.rewardtype = rewardtype;
    }

    public int getServerid() {
        return serverid;
    }

    public void setServerid(int serverid) {
        this.serverid = serverid;
    }

    //获取活动奖励信息接口,包涵初始化
    public static OpenServiceRushModule get(int serverid, activeControl sai){
        try {
            //从缓存拿奖励发放信息
            OpenServiceRushModule module = getv(key(serverid, String.format("RUSHREWARD:%s",sai.getActtype())));
            if(null == module){
                //初始化奖励发放信息
                module = create(serverid, sai);
            }
            return module;
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    static private String key(int serverid, int rankrushtype) {
        return key(serverid, "RUSHREWARD:" + rankrushtype);
    }

    /**
     * 奖励信息初始化
     */
    private static OpenServiceRushModule create(int serverid, activeControl sai) {
        if(sai == null) {
            return null;
        }

        //计算奖励发放时间,也就是活动真正的结束时间
        DateTime endtime = getRealEndDateTime(serverid, sai);
        //判断活动真实结束时间和策划配置的结束时间相比,如果真实结束时间晚于配置时间,提前发奖
        if(endtime.isAfter(sai.getEndtime())) {
            endtime = sai.getEndtime();
        }
        OpenServiceRushModule module = new OpenServiceRushModule();
        module.setServerid(serverid);//服
        module.setAward(false);//是否已经领奖
        module.setRewardTime(endtime);//设置活动真实结束时间,也就是领奖时间
        module.setRewardtype(Integer.parseInt(sai.getActtype()));//发放奖励类型,也就是活动类型1001,1002,1003,1004
        //初始化,存缓存
        module.set();
        return module;
    }

    //获取真实的活动结束时间
    private static DateTime getRealEndDateTime(int serverid, activeControl sai){
        //获取服务器开服时间信息
        ServerBean serverBean = ServerBean.get(serverid);
        DateTime starttime = serverBean.getStarttime();
        String s = starttime.toString("yyyy-MM-dd");
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
        starttime = DateTime.parse(s, format);
        //活动开启条件
        int daycondition = sai.getDaycondition();
        //计算真实活动开始时间=开服时间 + 活动开启条件表示的天数
        DateTime realStartDateTime = starttime.plusDays(daycondition);
        //活动运行天数
        int operationcycle = sai.getOperationcycle();
        // 真实活动结束时间 = 真实活动开始时间 + 活动运行天数
        DateTime realEndDateTime = realStartDateTime.plusDays(operationcycle);
        return realEndDateTime;
    }

    public void set() {
        try {
            setv(key(getServerid(),getRewardtype()), this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getServerid(),getRewardtype()));
    }
}
